SOLID Principles
··3 mins
Table of Contents
Go to References #
Videos #
- Bob Martin SOLID Principles of Object Oriented and Agile Design –
-
Software Design - Introduction to SOLID Principles in 8 Minutes - Quick, concise, and clear examples of each of the SOLID principles (though examples are simple and bookish):
- Single Responsibility - one reason to change; should be able to explain in a single sentence what this thing does
- Open Closed - closed for modification / open for extension (should not have to update if-else/switch blocks every time new types are added)
- Liskov Substitution - subtypes should be able to be swapped in for their base type –> should not unnecessarily overuse inheritance
- Interface Segregation - interfaces should be small –> should not have DUMMY implementations
- Dependency Inversion - objects should be created by caller, and callee should accept parent/generic types
- SOLID Design Principles in java with Example | JavaTechie - Longer explanation of SOLID principles with more realistic examples
Single Responsibility #
-
Robert C Martin - The Single Responsibility Principle | JavaTechie - Main issues with poorly structured code:
- Rigidity: tendency of the system to resist change, by forcing you to make the change in multiple places
- Fragility: a fix/change in one part of the codebase impacts an unrelated part of the codebase
- long distance couplings
- thing changed has multiple responsibilities that might seem similar, but are different
Open Closed #
- Never Rewrite Code (open/closed principle) | Code Walks 039 - Christopher Okhravi - Use wrappers and inject new objects, avoiding updating existing code by rewriting up the chain of dependencies (until you reach a point where you are sure there are no side effects)
Liskov Substitution #
- Understanding the Liskov Substitution Principle - EduTechional - Gives simple example in Ruby for how a child instance can break a program when it replaces the parent instance
-
Liskov Substitution Principle (SOLID), The Robustness Principle, and DbC | Code Walks 018 -
Christopher Okhravi - Think about typing along with the Robustness Principle, Pre-conditions, Post-conditions, and Invariants
- Robustness Principle / Postel’s Law: Be liberal in what you accept and conservative in what you send
- Pre-condition: What factors are true at start of method. Subtype should be able to accept everything base accepts and more (same or weaker)
- Post-condition: What factors are true at end of method. Subtype should not send anything that the base cannot send (same or stronger)
- Invariants: What factors are true at all times. Subtype should not change global state in ways that base cannot (same or stronger)
Interface Segregation #
-
Interface Segregation Principle Explained - SOLID Design Principles - Web Dev Simplified - Great example in Javascript showing how
Interface Segregation
keeps classes clean through not having unnecessary broken/not-implemented functionality - Interface Segregation Principle (SOLID) | Code Walks 023 - Christopher Okhravi - Regarding interfaces, too many is better than too few: this favors composition over inheritance, and smaller composed objects over monolithic objects / classes / services.
Dependency Inversion #
- Dependency Inversion - how, what, why? (examples in C#) | Code Walks 004 - Christopher Okhravi - Use abstractions wherever possible. Inject instances as high up in the program as possible. Improves code de-duplication, code re-use, testability (via mocking), and enables composition via inheritance.
- Connell Watkins - Onion Architecture with DDD and CQRS | DDD East Midlands Conference - Great comparison of various architectures and how they are similar based on Dependency Inversion, followed by a deep dive into DDD and CQRS